﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Data;
using System.Web;
using System.Data.SqlClient;

namespace ViVa.ActiveDirectory
{
    public class Logging
    {
        public int Type_ID { get; set; }
        public string Type_log { get; set; }
        public DateTime Wanneer { get; set; }
        public string Wie { get; set; }
        public string Wat { get; set; }
        public bool Writable { get; set; }
        public bool Custom_wie { get; set; }

        public static void Log(string userName, int Type_ID, string logMessage)
        {
                logMessage = logMessage.Replace("'", "''");
                string query = "INSERT INTO Logging (Type_ID, Wanneer, Wie, Wat)" +
                           "VALUES ('" + Type_ID + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + userName + "','" + logMessage + "')";
                Database.insertQueryDatabase(query);
        }
        
        public static DataTable getLogTypes()
        {
            string query = "SELECT Type_ID, Type_log, Writable, Custom_wie " +
                            "FROM Logging_Type " +
                            "ORDER BY Type_log";
            SqlDataReader reader = Database.queryDatabase(query);
            return Helper.ConvertSqlDataReaderToDataTable(reader);
        }

        public static DataTable getLog(int Type_ID, bool alleenTop3 = false)
        {
            string query = "select Logging.Type_ID, Logging_Type.Type_log, Logging.Wanneer, Logging.Wie, Logging.Wat, Logging_Type.Writable, Logging_Type.Custom_wie from Logging " +
                            "join Logging_Type " +
                            "on Logging.Type_ID = Logging_Type.Type_ID and Logging_Type.Type_ID = '" + Type_ID + "' " +
                            "ORDER BY Wanneer DESC";

            if (alleenTop3.Equals(true)) 
            {
              query = "select TOP 3 Logging.Type_ID, Logging_Type.Type_log, Logging.Wanneer, Logging.Wie, Logging.Wat, Logging_Type.Writable, Logging_Type.Custom_wie from Logging " +
                             "join Logging_Type " +
                             "on Logging.Type_ID = Logging_Type.Type_ID and Logging_Type.Type_ID = '" + Type_ID + "' " +
                             "ORDER BY Wanneer DESC";
            }

            SqlDataReader reader = Database.queryDatabase(query);
            return Helper.ConvertSqlDataReaderToDataTable(reader);
        }

        public static void deleteLog(Logging log)
        {
            log.Wat = log.Wat.Replace("'", "''");
            log.Wie = log.Wie.Replace("'", "''");
            string query = "DELETE FROM Logging " +
                            "WHERE Wie = '" + log.Wie + "' AND Wat = '" + log.Wat + "' AND Wanneer = '" + Convert.ToDateTime(log.Wanneer).ToString("yyyy-MM-dd HH:mm:ss") + "'";
            Database.insertQueryDatabase(query);
        }
    }
}
